package com.xbongbong.parent.abstracts;

import com.xbongbong.help.log.UpdateBatchLogHelp;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.ProSaveHelp;
import com.xbongbong.paas.pojo.AttrValuePojo;
import com.xbongbong.paas.pojo.dto.FormDataUpdateBatchDTO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateBatchVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.parent.interfaces.BusinessBatchEditService;
import com.xbongbong.parent.pojo.BatchEditValidateReturnPoJo;
import com.xbongbong.pro.permission.pojo.dto.VerifyUpdatePermissionDTO;
import com.xbongbong.sys.help.ProPermissionHelp;
import org.apache.commons.collections4.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @date 2022/11/17 14:23
 */
public abstract class AbstractBusinessBatchEditService<F, D> implements BusinessBatchEditService<F, D> {

    @Resource
    private UpdateBatchLogHelp updateBatchLogHelp;

    @Override
    public FormDataUpdateBatchVO updateBatch(FormDataUpdateBatchDTO formDataUpdateBatchDTO) throws XbbException {

        verifyEditPermission(formDataUpdateBatchDTO);

        List<D> formDataList = dataListAndSubDataList(formDataUpdateBatchDTO);

        List<FieldAttrEntity> explainList = getExplainList(formDataUpdateBatchDTO, formDataList);

        F formEntity = getFormEntity(formDataUpdateBatchDTO, formDataList);

        checkDataEditPermission(formDataUpdateBatchDTO, formEntity, formDataList);

        //权限校验过滤不满足的数据，后面优化封装报错，现在先返回
        if(CollectionUtils.isEmpty(formDataList)){
            return new FormDataUpdateBatchVO();
        }

        BatchEditValidateReturnPoJo batchEditValidateReturnPoJo = validateData(formDataUpdateBatchDTO, explainList, formDataList);

        FormDataUpdateBatchVO formDataUpdateBatchVO = batchEditAttr(formDataUpdateBatchDTO, batchEditValidateReturnPoJo, formDataList, formEntity, explainList);
        //设置返回值
        List<AttrValuePojo> changedValueList = ProSaveHelp.analysisData(batchEditValidateReturnPoJo.getDummyAttr2ValueMap(), batchEditValidateReturnPoJo.getEditedFieldExplainMap(), formDataUpdateBatchDTO.getCorpid());
        formDataUpdateBatchVO.setChangedValueList(changedValueList);
        after(formDataUpdateBatchDTO, formDataList, formEntity);


        return formDataUpdateBatchVO;
    }

    @Override
    public void verifyEditPermission(FormDataUpdateBatchDTO formDataUpdateBatchDTO) throws XbbException {
        VerifyUpdatePermissionDTO verifyUpdatePermissionDTO = new VerifyUpdatePermissionDTO();
        BeanUtil.copyProperties(formDataUpdateBatchDTO, verifyUpdatePermissionDTO);
        ProPermissionHelp.verifyEditPermission(verifyUpdatePermissionDTO);
    }


    /**
     * 过滤 单选、复选框、下拉框、下拉复选  、应收账期等
     * @param editedAttr2ValueMap
     * @param editedFieldExplainMap
     */
    protected void filterValue4UpdateBatch(Map<String, Object> editedAttr2ValueMap, Map<String, FieldAttrEntity> editedFieldExplainMap) throws XbbException {
        for (Map.Entry<String, Object> item : editedAttr2ValueMap.entrySet()) {
            Object value = ProSaveHelp.getValue(item.getValue(), item.getKey(), editedFieldExplainMap);
            item.setValue(value);
        }
    }
    /**
     * 设置修改后的数据以及 LogValue (解析不同的数据结构)
     * @param editedAttr2ValueMap
     * @param dummyAttr2ValueMap
     * @param explainMap
     * @param formDataUpdateBatchDTO
     * @throws XbbException
     */
    protected void setBatchEditDataAndLog(Map<String, Object> editedAttr2ValueMap, Map<String, Object> dummyAttr2ValueMap, Map<String, FieldAttrEntity> explainMap, FormDataUpdateBatchDTO formDataUpdateBatchDTO) throws XbbException {
        List<AttrValuePojo> logFiledEditedList = new ArrayList<>();
        for (Map.Entry<String, Object> av : dummyAttr2ValueMap.entrySet()) {
            String attr = av.getKey();
            Object value = av.getValue();
            AttrValuePojo logPojo = updateBatchLogHelp.getLogPojo(explainMap, attr, value);
            logFiledEditedList.add(logPojo);
        }

        List<AttrValuePojo> fieldEditedList = new ArrayList<>();
        for (Map.Entry<String, Object> entry : editedAttr2ValueMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            AttrValuePojo avPojo = new AttrValuePojo();
            avPojo.setAttr(key);
            avPojo.setValue(value);
            fieldEditedList.add(avPojo);
        }
        formDataUpdateBatchDTO.setLogFieldEditedList(logFiledEditedList);
        formDataUpdateBatchDTO.setFieldEditedList(fieldEditedList);
    }

}
